package com.xl98.config;

import lombok.Value;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShrioConfig {
    @Bean
    public ShiroFilterFactoryBean shirFilter(DefaultWebSecurityManager securityManager){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        // 必须设置 SecurityManager
        shiroFilterFactoryBean.setSecurityManager(securityManager);

        // 设置拦截器
        Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
        //开放上传文章接口
        filterChainDefinitionMap.put("/api/article/editor/upload/**", "anon");
        filterChainDefinitionMap.put("/upload/**", "anon");
        filterChainDefinitionMap.put("/api/article/count/**", "anon");
        //开放swagger
        filterChainDefinitionMap.put("/doc.html", "anon");
        filterChainDefinitionMap.put("/webjars/**", "anon");
        filterChainDefinitionMap.put("/v2/**", "anon");
        filterChainDefinitionMap.put("/swagger-resources/**", "anon");
        //开放邮箱注册
        filterChainDefinitionMap.put("/api/user/email/**", "anon");
        filterChainDefinitionMap.put("/api/user/code/**", "anon");
        //开放管理用户
        filterChainDefinitionMap.put("/api/user/all/**", "anon");
        filterChainDefinitionMap.put("/api/user/one/**", "anon");
        filterChainDefinitionMap.put("/api/user/updateuser/**", "anon");
        filterChainDefinitionMap.put("/api/user/verifyemail/**", "anon");
        filterChainDefinitionMap.put("/api/user/userRole/**", "anon");
        filterChainDefinitionMap.put("/api/user/userstate/**", "anon");
        //开放管理树洞
        filterChainDefinitionMap.put("/api/treehole/find/**", "anon");
        filterChainDefinitionMap.put("/api/treehole/add/**", "anon");
        filterChainDefinitionMap.put("/api/treehole/update/**", "anon");
        //开放管理吐槽
        filterChainDefinitionMap.put("/api/complain/find/**", "anon");
        filterChainDefinitionMap.put("/api/complain/add/**", "anon");
        filterChainDefinitionMap.put("/api/complain/update/**", "anon");
        //开放评论
        filterChainDefinitionMap.put("/api/comment/add/**", "anon");
        filterChainDefinitionMap.put("/api/comment/all/**", "anon");
        filterChainDefinitionMap.put("/api/comment/count/**", "anon");

        //开放角色管理
        filterChainDefinitionMap.put("/api/role/all/**", "anon");
        filterChainDefinitionMap.put("/api/user-role/add/**", "anon");
        filterChainDefinitionMap.put("/api/role/userRoles/**", "anon");
        //开放权限管理
        filterChainDefinitionMap.put("/ api/perm/all/**", "anon");
        filterChainDefinitionMap.put("/ api/perm/add/**", "anon");
        filterChainDefinitionMap.put("/ api/role-perm/add/**", "anon");
        filterChainDefinitionMap.put("/ api/perm/rolePerm/**", "anon");
        //开放状态管理
        filterChainDefinitionMap.put("/api/state/all/**", "anon");

   /*     //游客，开发权限
        filterChainDefinitionMap.put("/guest/**", "anon");
        //用户，需要角色权限 “user”
        filterChainDefinitionMap.put("/user/**", "roles[user]");
        //管理员，需要角色权限 “admin”
        filterChainDefinitionMap.put("/admin/**", "roles[admin]");*/
        //开放登陆接口
        filterChainDefinitionMap.put("/api/shiro/login", "anon");
        //开放注册接口
        filterChainDefinitionMap.put("/api/user/regist/**", "anon");

        //开放退出登录接口
        filterChainDefinitionMap.put("/api/shiro/logout", "anon");


        //其余接口一律拦截
        //filterChainDefinitionMap.put("/**", "authc");
        //filterChainDefinitionMap.put("/**", "anon");
        //配置shiro默认登录界面地址，前后端分离中登录界面跳转应由前端路由控制，后台仅返回json数据
        shiroFilterFactoryBean.setLoginUrl("/unauth");

        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        System.out.println("Shiro拦截器工厂类注入成功");
        return shiroFilterFactoryBean;
    }
    @Bean
    public DefaultWebSecurityManager securityManager(){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        //设置REALM
        securityManager.setRealm(customRealm());
        return securityManager;
    }
    @Bean
    public MyShiroRealm customRealm(){
        return new MyShiroRealm();
    }
}
